/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.custom.web;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.custom.entity.CodeRule;
import com.jeesite.modules.custom.service.CodeRuleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 编码规则Controller
 * @author Kilo
 * @version 2021-01-22
 */
@Controller
@RequestMapping(value = "${adminPath}/custom/codeRule")
public class CodeRuleController extends BaseController {

	@Autowired
	private CodeRuleService codeRuleService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public CodeRule get(String id, boolean isNewRecord) {
		return codeRuleService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("custom:codeRule:view")
	@RequestMapping(value = {"list", ""})
	public String list(CodeRule codeRule, Model model) {
		model.addAttribute("codeRule", codeRule);
		return "modules/custom/codeRuleList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("custom:codeRule:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<CodeRule> listData(CodeRule codeRule, HttpServletRequest request, HttpServletResponse response) {
		codeRule.setPage(new Page<>(request, response));
		Page<CodeRule> page = codeRuleService.findPage(codeRule);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("custom:codeRule:view")
	@RequestMapping(value = "form")
	public String form(CodeRule codeRule, Model model) {
		model.addAttribute("codeRule", codeRule);
		return "modules/custom/codeRuleForm";
	}

	/**
	 * 查看表单详情
	 */
	@RequiresPermissions("custom:codeRule:view")
	@RequestMapping(value = "detail")
	public String detail(CodeRule codeRule, Model model) {
		model.addAttribute("codeRule", codeRule);
		return "modules/custom/codeRuleDetail";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("custom:codeRule:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated CodeRule codeRule) {
		codeRuleService.save(codeRule);
		return renderResult(Global.TRUE, text("保存编码规则成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("custom:codeRule:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(CodeRule codeRule) {
		codeRuleService.delete(codeRule);
		return renderResult(Global.TRUE, text("删除编码规则成功！"));
	}

	/**
	 * 重置编号
	 * @param ids
	 */
	@RequiresPermissions("custom:codeRule:edit")
	@RequestMapping(value = "reset")
	@ResponseBody
	public String reset(String[] ids) {
		for (String id : ids) {
			CodeRule codeRule = new CodeRule();
			codeRule.setId(id);
			codeRule.setCurrentNumber(0L);
			codeRuleService.update(codeRule);
		}
		return renderResult(Global.TRUE, text("重置编号成功"));
	}
}